//
//  MenuViewController.swift
//  ifanr
//
//  Created by 😱😱 on 16/9/13.
//  Copyright © 2016年 😱😱. All rights reserved.
//

import UIKit

class MenuViewController: UIViewController {

    // MARK:--------------------------- LifeCycle --------------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(backgroundImageView)
        self.view.addSubview(menuTableView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //MARK:---------------------------        UI       --------------------------
    private lazy var backgroundImageView : UIImageView = {
        var backgroundImageView = UIImageView(frame: self.view.bounds)
        backgroundImageView.contentMode = .ScaleAspectFill
        backgroundImageView.image = UIImage(named: "profile_background")
        return backgroundImageView
    }()
    
    /// tableview
    private lazy var menuTableView : UITableView = {
        var menuTableView : UITableView = UITableView(frame: self.view.bounds)
        menuTableView.backgroundColor = UIColor.clearColor()
        let headerView = MenuHeaderView(frame: CGRect(x: 0, y: 0, width: UIConstant.SCREEN_WIDTH, height: 100))
        headerView.delegate = self
        menuTableView.tableHeaderView = headerView
        menuTableView.separatorStyle = .None
        menuTableView.rowHeight   = 80
        menuTableView.sectionHeaderHeight = 100
        menuTableView.dataSource  = self
        menuTableView.delegate    = self
        return menuTableView
    }()
}

//MARK:---------------------------menuTableView--------------------------

extension MenuViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return MenuTabItems.count
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = MenuTableViewCell.cellWithTableView(tableView)
        cell.model = MenuTabItems[indexPath.row]
        cell.selectionStyle = UITableViewCellSelectionStyle.None
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        self.showTips(MenuTabItems[indexPath.row].title, duration: 2)
    }
}

extension MenuViewController: MenuHeaderViewDelegate {
    func searchBtnDidClick(headerView: MenuHeaderView, searchBtn: UIButton) {
        self.showTips("搜索按钮", duration: 2)
    }
    func settingBtnDidClick(headerView: MenuHeaderView, settingBtn: UIButton) {
        self.navigationController?.pushViewController(SettingViewController(), animated: true)
    }
}